//
//  RadioListViewController.m
//  瓜子电台
//
//  Created by Jackson on 16/8/10.
//  Copyright © 2016年 YueZhang. All rights reserved.
//

#import "RadioListViewController.h"
#import "RadioTableViewCell.h"
@interface RadioListViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong) NSMutableArray *dataArray;

@property(nonatomic,strong) UITableView *tableView;



@end

@implementation RadioListViewController

-(NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
    
}


-(void)getData {
    NSString *url = [[NSString alloc]init];
    
    if (self.categoryId != 0) {
        url = [NSString stringWithFormat:@"http://live.ximalaya.com/live-web/v2/radio/category?categoryId=%ld&device=iPhone&pageNum=1&pageSize=30", self.categoryId];
        self.navigationItem.title = self.topTitleName;
    } else if (self.locationId != 0) {
        url = @"http://live.ximalaya.com/live-web/v1/radio/local?device=iPhone&pageNum=1&pageSize=30";
        self.navigationItem.title = @"你好·上海";
    } else if (self.rankId != 0) {
        url = @"http://live.ximalaya.com/live-web/v3/radio/hot?device=iPhone&pageNum=1&pageSize=30";
        self.navigationItem.title = @"电台排行榜";
    } else if (self.topBtnTag == 0) {
        url = @"http://live.ximalaya.com/live-web/v2/radio/province?device=iPhone&pageNum=1&pageSize=30&provinceCode=310000&statEvent=pageview%2Fradiolist%40%E6%9C%AC%E5%9C%B0%E5%8F%B0&statModule=%E6%9C%AC%E5%9C%B0%E5%8F%B0&statPage=tab%40%E5%8F%91%E7%8E%B0_%E5%B9%BF%E6%92%AD";
        self.navigationItem.title = @"本地台";
    } else if (self.topBtnTag == 1) {
        url = @"http://live.ximalaya.com/live-web/v2/radio/national?device=iPhone&pageNum=1&pageSize=30&statEvent=pageview%2Fradiolist%40%E5%9B%BD%E5%AE%B6%E5%8F%B0&statModule=%E5%9B%BD%E5%AE%B6%E5%8F%B0&statPage=tab%40%E5%8F%91%E7%8E%B0_%E5%B9%BF%E6%92%AD";
        self.navigationItem.title = @"国家台";
    } else if (self.topBtnTag == 2) {
        url = @"http://live.ximalaya.com/live-web/v2/radio/network?device=iPhone&pageNum=1&pageSize=30&statEvent=pageview%2Fradiolist%40%E7%BD%91%E7%BB%9C%E5%8F%B0&statModule=%E7%BD%91%E7%BB%9C%E5%8F%B0&statPage=tab%40%E5%8F%91%E7%8E%B0_%E5%B9%BF%E6%92%AD";
        self.navigationItem.title = @"网络台";
    } else {
        //        NSLog(@"cuo wu");
    }
    [NetWorkrequestManage requestWithType:GET url:url parameters:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.dataArray = [RadioListModel modelConfigureWithDic:dic];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self creatTableView];
            [self reloadInputViews];
        });
    } error:^(NSError *error) {
        NSLog(@"解析出错");
    }];
}

-(void)creatTableView {
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"mohu.png"]];
    self.tableView.rowHeight = 100;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"RadioTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.tableView];

    //创建下拉刷新
    MJRefreshNormalHeader* header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self performSelector:@selector(headRefresh)withObject:nil afterDelay:2.0f];
        
    }];
    
    //设置自定义文字，因为默认是英文的
    [header setTitle:@"下拉刷新"forState:MJRefreshStateIdle];
    
    [header setTitle:@"松开加载更多"forState:MJRefreshStatePulling];
    
    [header setTitle:@"正在刷新中"forState:MJRefreshStateRefreshing];
    
    self.tableView.mj_header= header;
    
    //创建上拉刷新
    MJRefreshBackNormalFooter * foot =[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        [self performSelector:@selector(footRefresh)withObject:nil afterDelay:2.0f];
        
    }];
    self.tableView.mj_footer= foot;
    
    [foot setTitle:@"上拉刷新"forState:MJRefreshStateIdle];
    
    [foot setTitle:@"松开加载更多"forState:MJRefreshStatePulling];
    
    [foot setTitle:@"正在刷新中"forState:MJRefreshStateRefreshing];
    
}


- (void)headRefresh {
    NSLog(@"下拉,加载数据");
    [self.tableView.mj_header endRefreshing];
}
- (void)footRefresh {
    NSLog(@"上拉，加载数据");
    [self.tableView.mj_footer endRefreshing];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  self.dataArray.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RadioTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    RadioListModel *model = self.dataArray[indexPath.row];
    [cell cellConfigureWithRadioModel:model];
    cell.playButton.tag = indexPath.row + 100;
    [cell.playButton addTarget:self action:@selector(playaction:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

-(void)playaction:(UIButton *)button {
    NSLog(@"音乐播放");
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"跳转到播放");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
